<template>
  <p-modal
    :title="title"
    :width="600"
    :visible="visible"
    :maskClosable="false"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel">
    <p-spin :spinning="confirmLoading">
  <!-- 主表单区域 -->
  <p-form :form="form">
    <p-row>

	  <p-col :span="24">
      <p-form-item label="工序代码" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'processmcode', validatorRules.processmcode]" placeholder="请输入工序代码"></p-input>
      </p-form-item>
      </p-col>
        <p-col :span="24">
        <p-form-item label="工序名称 " :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'processmname', validatorRules.processmname]" placeholder="请输入工序名称"></p-input>
        </p-form-item>
          </p-col>
        <p-col :span="24">
        <p-form-item label="工序描述" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'processsprc', validatorRules.processsprc]" placeholder="请输入工序描述"></p-input>
        </p-form-item>
          </p-col>
        <p-col :span="24">
          <p-form-item label="工序层级" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <p-input v-decorator="[ 'processsprc', validatorRules.processsprc]" placeholder="请输入工序层级"></p-input>
          </p-form-item>
        </p-col>
        <p-col :span="24">
          <p-form-item label="资源类型" :labelCol="labelCol" :wrapperCol="wrapperCol">  
            <p-select v-decorator="[ 'pid', validatorRules.pid]">
              <p-select-option :value="item.id" v-for="(item,index) in processlayerArr" :key="index">
                {{item.processmname}}
              </p-select-option>
            </p-select>
          </p-form-item>
        </p-col>
        <p-col :span="24">
          <p-form-item label="资源名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <!-- <p-input v-decorator="[ 'productffset', validatorRules.productffset]" placeholder="请输入产品描述"></p-input> -->
            <p-input-number v-decorator="[ 'productffset', validatorRules.productffset]" />            
          </p-form-item>
        </p-col>
        <p-col :span="24">
          <p-form-item label="资源规格型号" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <p-select v-decorator="[ 'distribution', validatorRules.productffset]">
              <p-select-option :value= '1'>
                合并
              </p-select-option>
              <p-select-option :value= '2'>
                不合并
              </p-select-option>
            </p-select>             
          </p-form-item>
        </p-col>

        <p-col :span="24">
          <p-form-item label="数量" :labelCol="labelCol" :wrapperCol="wrapperCol">
            <!-- <p-input v-decorator="[ 'productffset', validatorRules.productffset]" placeholder="请输入产品描述"></p-input> -->
            <p-input-number v-decorator="[ 'productffset', validatorRules.productffset]" />            
          </p-form-item>
        </p-col>

        </p-row>
      </p-form>

    </p-spin>
  </p-modal>
</template>

<script>

  import pick from 'lodash.pick'
  import { FormTypes,getRefPromise } from '@/utils/JEditableTableUtil'
  import { JEditableTableMixin } from '@/mixins/JEditableTableMixin'
  import { validateDuplicateValue } from '@/utils/util'
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import { postAction, getAction, deleteAction } from '@/api/manage'


  export default {
    name: 'MesPeProcessMaterialBasisModal',
    mixins: [JEditableTableMixin],
    components: {
      JDictSelectTag,
    },
    data() {
      return {
        selectdePid: null,
        processlayerArr: [],
        testVal: 1,
        labelCol: {
          span: 6
        },
        wrapperCol: {
          span: 16
        },
        labelCol2: {
          span: 3
        },
        wrapperCol2: {
          span: 20
        },
        // 新增时子表默认添加几行空数据
        addDefaultRowNum: 1,
        validatorRules: {
          processmcode: {rules: [
            {required: true, message: '请输入工序编码!'},
          ]},
          processmname: {rules: [
            {required: true, message: '请输入工序名称!'},
          ]},
          processsprc: {rules: [
            {required: true, message: '请输入工序描述!'},
          ]},
          processlayer: {rules: [
            {required: true, message: '请输入工序层级!'},
          ]},
          pid: {rules: [
            {required: true, message: '请输入父级工序!'},
          ]},
          productffset: {rules: [
            {required: true, message: '请输入偏置期!'},
          ]},
        },
        refKeys: ['mesPeProductProcessRoute', ],
        tableKeys:['mesPeProductProcessRoute', ],
        activeKey: 'mesPeProductProcessRoute',
        // 基础信息工艺路线
        mesPeProductProcessRouteTable: {
          loading: false,
          dataSource: [],
          columns: [
            {
              title: '产品系列',
              key: 'productLine',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '工艺路线内码',
              key: 'processid',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
            },
            {
              title: '工艺路线编码',
              key: 'processCoding',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '工艺路线名称',
              key: 'processName',
              type: FormTypes.input,
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
            {
              title: '工艺路线类型',
              key: 'processType',
              type: FormTypes.select,
              dictCode: '',
              width: '200px',
              placeholder: '请输入${title}',
              defaultValue: '',
              validateRules: [{ required: true, message: '${title}不能为空' }],
            },
          ]
        },
        url: {
          add: '/mes-craft/craft/mesPeDefinition/add',
          edit: '/mes-craft/craft/mesPeDefinition/edit',
          pid: '/mes-craft/craft/mesPeDefinition/getParentByDefinitionLayer',
          mesPeProductProcessRoute: {
            list: '/mes-craft/craft/mesPeDefinition/list'
          },
        }
      }
    },
    methods: {

      getPid(val){
        this.selectdePid = val
        const param = {
          processLayer: val
        }
        getAction(this.url.pid,  param).then((res) => {
          this.processlayerArr = res.data
        })
      },

      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
              that.confirmLoading = true;
              let httpurl = '';
              let method = '';
              let formData = Object.assign(this.model, values);
              this.formData = Object.assign(this.model, values);
              //新增触发
              if(!this.model.id){
                httpurl+=this.url.add;
                method = 'post';
                if(this.EnhanceJS.afterAdd){
                  //新增之前
                  let Obj = eval(this.EnhanceJS.afterAdd)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }else{
                //编辑触发
                httpurl+=this.url.edit;
                method = 'put';
                if(this.EnhanceJS.afterEdit){
                 //编辑之前
                  let Obj = eval(this.EnhanceJS.afterEdit)
                  new Obj(this)
                }else{
                  console.log("表单提交数据",formData)
                  that.modalFrom(httpurl,formData,method)
                }
              }
          }

        })
      },

      getAllTable() {
        let values = this.tableKeys.map(key => getRefPromise(this, key))
        return Promise.all(values)
      },
      /** 调用完edit()方法之后会自动调用此方法 */
      editAfter() {
        let fieldval = pick(this.model,'processmcode','processmname','processsprc','processlayer','pid','productffset', 'distribution')
        this.$nextTick(() => {
          this.form.setFieldsValue(fieldval)
        })
        // 加载子表数据
        if (this.model.id) {
          let params = { id: this.model.id }
          this.requestSubTableData(this.url.mesPeProductProcessRoute.list, params, this.mesPeProductProcessRouteTable)
        }
      },
      /** 整理成formData */
      classifyIntoFormData(allValues) {
        let main = Object.assign(this.model, allValues.formValue)

        return {
          ...main, // 展开
          mesPeProductProcessRouteList: allValues.tablesValue[0].values,
        }
      },
      validateError(msg){
        this.$message.error(msg)
      },
     popupCallback(row){
       this.form.setFieldsValue(pick(row,'mcode','mname','sprc','mexplain','productLine','version','processPt','processStatus'))
     },

    }
  }
</script>

<style scoped>
</style>